/**
 * Add your package below. Package name can be found in the project's AndroidManifest.xml file.
 * This is the package name our example uses:
 * <p>
 * package com.example.android.justjava;
 */
package com.example.android.justjava;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {
    /**
     * Quantity is a global variable.
     */
    int quantity = 0;
    String order_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        int price = calculatePrice();
        order_message = createOrderSummary(price);
        displayMessage(order_message);

        sendEmailOrder();
    }

    /**
     * Create an email order.
     */
    private void sendEmailOrder() {
        // create an email Order
        Intent intent = new Intent(Intent.ACTION_SENDTO);

        String mailList = "qinwen_luo@foxmail.com;";// create email list
        intent.setData(Uri.parse("mailto:" + mailList)); // only email apps should handle this

        intent.putExtra(Intent.EXTRA_SUBJECT, "Coffee Order by " + getName());
        intent.putExtra(Intent.EXTRA_TEXT, order_message);

        // check if exist an email application to receive the intent
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    /**
     * Create an order summary.
     *
     * @param price is the price of coffee ordered
     * @return priceMessage is the order summary returned
     */
    private String createOrderSummary(int price) {
        String priceMessage = getString(R.string.name) + ":" + getName() + "\n";
        priceMessage += getString(R.string.whipped_cream) + ":" + getWhippedCream() + "\n";
        priceMessage += getString(R.string.chocolate) + ":" + getChocolate() + "\n";
        priceMessage += getString(R.string.quantity) + ":" + quantity + "\n";
        priceMessage += getString(R.string.total) + price + "\n";
        priceMessage += getString(R.string.thanks);
        return priceMessage;
    }

    /**
     * Calculates the price of the order.
     *
     * @return price is the price of coffee
     */
    private int calculatePrice() {
        int basePrice = 5;

        if (getWhippedCream()) {
            basePrice += 1;
        }
        if (getChocolate()) {
            basePrice += 2;
        }
        return basePrice * quantity;
    }

    /**
     * This method is called when the add button is clicked.
     */
    public void increment(View view) {
        if (quantity < 100) {
            quantity = quantity + 1;
        } else {
            Toast.makeText(this, getString(R.string.over_limits_toast), Toast.LENGTH_SHORT).show();
        }
        displayQuantity(quantity);
    }

    /**
     * This method is called when the minus button is clicked.
     */
    public void decrement(View view) {
        if (quantity > 0) {
            quantity = quantity - 1;
        } else {
            Toast.makeText(this, getString(R.string.under_limits_toast), Toast.LENGTH_SHORT).show();
        }
        displayQuantity(quantity);
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayQuantity(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }

    /**
     * This method displays the given text on the screen.
     */
    private void displayMessage(String message) {
        TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
        orderSummaryTextView.setText(message);
    }

    /**
     * This method get the whippedCream state value
     *
     * @return whippedCream state
     */
    private boolean getWhippedCream() {
        CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
        return whippedCreamCheckBox.isChecked();
    }

    /**
     * This method get the whippedCream state value
     *
     * @return whippedCream state
     */
    private boolean getChocolate() {
        CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
        return chocolateCheckBox.isChecked();
    }

    private Editable getName() {
        EditText nameEditText = (EditText) findViewById(R.id.name_edit_text);
        return nameEditText.getText();
    }
}
